package com.qianbao.qianbaobusiness.ui.qrcode.adapter;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidbase.dagger.help.GlideApp;
import com.androidbase.widget.recycleadpter.BaseRecycleViewAdapter;
import com.qianbao.qianbaobusiness.R;
import com.qianbao.qianbaobusiness.json.QrBean;
import com.qianbao.qianbaobusiness.ui.qrcode.BindQrCodeActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by hepeng on 2018/6/22.
 */

public class QrAdapter extends BaseRecycleViewAdapter<QrBean> {
    public QrAdapter(Context context) {
        super(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onBindBaseViewHolder(RecyclerView.ViewHolder holder, QrBean item) {
        QrViewHolder qrViewHolder = (QrViewHolder) holder;
        GlideApp.with(mContext).load(item.originalUrl)
                .fallback(mContext.getDrawable(R.drawable.error_qr))
                .error(mContext.getDrawable(R.drawable.error_qr))
                .into(((QrViewHolder) holder).qr_icon);
        qrViewHolder.tv_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((BindQrCodeActivity) mContext).startDownLoad(item.actualUrl, item.actualUrl.substring(item.actualUrl.lastIndexOf("/")), item.qrCode);
            }
        });
        qrViewHolder.tv_unbind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((BindQrCodeActivity) mContext).merchantQrcodeRemoveBind(item);
            }
        });
    }

    @Override
    protected RecyclerView.ViewHolder onCreateBaseViewHolder(ViewGroup parent, int viewType) {
        return new QrViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_qr_list, parent, false));
    }

    @Override
    protected int getBaseItemViewType(int position) {
        return 0;
    }

    public class QrViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.qr_icon)
        ImageView qr_icon;
        @BindView(R.id.tv_save)
        TextView  tv_save;
        @BindView(R.id.tv_unbind)
        TextView  tv_unbind;

        public QrViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
